package news.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import news.dao.NewsDao;
import news.model.News;
import news.model.NewsType;
import news.util.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class NewsService {

    @Autowired
    private NewsDao newsDao;

    public PageInfo<News> roleList(News news){
        PageHelper.startPage(news.getPageNum(), news.getPageSize());
        List<News> newsList = newsDao.newsList(news);
        PageInfo<News> pageInfo = new PageInfo<>(newsList);
        return pageInfo;
    }

    //新增时查询所有得newstype
    public List<NewsType> newsType(){
        List<NewsType> newsTypes = newsDao.newsType();
        return newsTypes;
    }

    //改变状态
    public void changeStatus(Integer status,Integer id){
        newsDao.changeStatus(status,id);
    }

    public void saveNews(News news, String adminToken) {
        //从token中解析出管理员id
        news.setAdmin(JWTUtil.getAdminByToken(adminToken));
        newsDao.saveNews(news);
    }

    public void delete(Integer id) {
        newsDao.delete(id);
    }

    public void update(News news, String adminToken) {
        news.setAdmin(JWTUtil.getAdminByToken(adminToken));
        newsDao.update(news);
    }

    public News findNewsById(int id) {
        News news = newsDao.findNewsById(id);
        return news;
    }
}
